package Hxw3DEngine.core.Resource.texture
{
	import flash.display3D.Context3D;
	import flash.display3D.textures.Texture;
	import flash.display3D.textures.TextureBase;
	import flash.display3D.Context3DTextureFormat;
	import Hxw3DEngine.asset.parsers.texture.BitmapVO;
	import Hxw3DEngine.core.Resource.texture.ResourceTexture;
	
	import flash.display.BitmapData;
	import Hxw3DEngine.utils.Stage3dUtils;
	import Hxw3DEngine.proxy.Stage3DProxy;
	
	public class BitmapTexture extends ResourceTexture
	{
		private var bitmapdata:BitmapData;

		// 兼容以前接口
		static public function create(bd:BitmapData,mipmap:Boolean = true):BitmapTexture
		{
			var bt:BitmapTexture = new BitmapTexture();
			bt.createFromVO(new BitmapVO(bd,mipmap));
			return bt;
		}
		
		public function BitmapTexture()
		{
		}
		override public function createFromVO(vo:Object):Boolean
		{
			var voBt:BitmapVO = vo as BitmapVO;
			bitmapdata = voBt.bitmapdata;
			_width = bitmapdata.width;
			_height = bitmapdata.height;	
			mipmap = voBt.mipmap;
			format = Context3DTextureFormat.BGRA;
			
			texture = Stage3DProxy.context3d.createTexture(width, height, format, false);
			if (texture == null) {
				return false;
			}
			if(mipmap)
				Stage3dUtils.uploadMipMap(Texture(texture),bitmapdata);
			else
				Texture(texture).uploadFromBitmapData(bitmapdata, 0);
			return true;
		}
		override public function dispose():void 
		{
			super.dispose();
			bitmapdata.dispose();
		}
	}
}